/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author phat10130059
 */
public class UserComputerUtil {

    public static List<String> getAllUserComputer() {
        List<String> listUser = new ArrayList<String>();

        ProcessBuilder processBuilder = new ProcessBuilder("net", "user");
        List<String> list = runProcessAndReturnOutput(processBuilder);

        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add(list.get(0));
        arrayList.add(list.get(1));
        arrayList.add(list.get(list.size() - 1));

        list.removeAll(arrayList);
        
        
        for (int i = 0; i < list.size(); i++) {
            List<String> cutStringEmply = StringUtil.cutStringEmptyLength(list.get(i));
            for (String string : cutStringEmply) {
                if (!string.trim().equals("")) {
                    listUser.add(string.trim());
                }

            }

        }
        return listUser;
    }

    public static List<String> runProcessAndReturnOutput(ProcessBuilder processBuilder) {

        List<String> list = new ArrayList<String>();
        try {

            String line;
            processBuilder.redirectErrorStream(true);
            Process p = processBuilder.start();
            BufferedReader input =
                    new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((line = input.readLine()) != null) {
                if (!line.trim().equals("")) {
                    list.add(line);
//                    String toLowerCase = line.replaceAll("\\s{10}", " ").toLowerCase();
//                    System.out.println(toLowerCase);
                }

            }
            input.close();
        } catch (Exception err) {
            err.printStackTrace();
        }

        return list;
    }
    
//    public static void main(String argv[]) throws UnknownHostException {
//        List<String> allUserComputer = getAllUserComputer();
//        for (String string : allUserComputer) {
//            System.out.println(string);
//        }
//    }
    
}
